import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Role,ROLES_KEY } from './roles';

@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private reflector: Reflector) {}
  canActivate(context: ExecutionContext): boolean {
    const roles = this.reflector.getAllAndOverride<Role[]>(
        ROLES_KEY, [
            context.getHandler(),
            context.getClass(),
        ]
    );
    if (!roles) {
      return true;
    }
    const user = context.switchToHttp().getRequest();
    if(user){
      console.info(user)
      return roles.some((role) => role === user.body.role);
    }else{
      return false
    }
  }
}

